package com.goudan.practice.array;

import java.util.ArrayList;
import java.util.List;

/**
 * 找到数组中所有和为K的集合
 */
public class FindAllSum {

    public static void main(String[] args) {
        String str = "1234";
        List<String> ans = new ArrayList<>();
        String path = "";
        process(str.toCharArray(), 0, ans, path, 5);
        System.out.println(ans);;

    }

    public static void process(char[] str, int index, List<String> ans, String path, int tag) {
        if (index == str.length && !String.valueOf(str).equals("")) {
            int sum = 0;
            for (char c : path.toCharArray()) {
                sum  = sum + Integer.parseInt(String.valueOf(c));
            }
            if (sum == tag) {
                ans.add(path);
            }
            return;
        }
        // 没有要index位置的字符
        process(str, index + 1, ans, path, tag);
        // 要了index位置的字符
        process(str, index + 1, ans, path + String.valueOf(str[index]), tag);
    }

}
